package com.lfg.leetcode.leetcode0;

public class No92reverseBetween {

    public static void main(String[] args) {
        ListNode ret = reverseBetween(ListNode.deserialize("[3,5]"), 1, 2);
        while (ret != null) {
            System.out.println("[" + ret.val + "]");
            ret = ret.next;
        }
    }

    public static ListNode reverseBetween(ListNode head, int m, int n) {
        if (m <= 0 || n <= 0 || n <= m) {
            return head;
        }
        ListNode preNode = null;
        ListNode newTail = null;
        ListNode newList = null;
        ListNode next = null;

        int index = 1;
        ListNode ret = head;
        while (head != null) {
            next = head.next;
            if (index == m) {
                newTail = head;
                newList = head;
                newList.next = null;
            } else if (index > m) {
                head.next = newList;
                newList = head;
            } else if (index < m) {
                preNode = head;
            }
            head = next;

            if (index == n) {
                break;
            }

            index++;
        }
        if (newTail != null) {
            newTail.next = next;
        }
        if (null != preNode) {
            preNode.next = newList;
        }else{
            return  newList;
        }
        return ret;
    }

    public static ListNode reverseBetween2(ListNode head, int m, int n) {
        if (m <= 0 || n <= 0 || n <= m) {
            return head;
        }
        ListNode pre1 = null;
        ListNode pre2 = null;
        ListNode node1 = null;
        ListNode node2 = null;
        int index = 1;
        ListNode ret = head;
        while (head != null) {
            if (index + 1 == m) {
                pre1 = head;
            }
            if (index == m) {
                node1 = head;
            }
            if (index + 1 == n) {
                pre2 = head;
            }
            if (index == n) {
                node2 = head;
                break;
            }
            head = head.next;
            index++;
        }
        if (null == node1 || null == node2 || null == pre2) {
            return ret;
        }
        if (pre1 == null) {
            if (node1 == pre2) {
                node1.next = node2.next;
                node2.next = node1;
                return node2;
            }
            ListNode next1 = node1.next;
            ListNode next2 = node2.next;
            pre2.next = node1;
            node2.next = next1;
            node1.next = next2;
            return node2;
        }
        if (node1 == pre2) {
            ListNode next2 = node2.next;
            pre1.next = node2;
            node2.next = node1;
            node1.next = next2;
            return ret;
        }
        ListNode next1 = node1.next;
        ListNode next2 = node2.next;
        node2.next = next1;
        node1.next = next2;
        pre1.next = node2;
        pre2.next = node1;

        return ret;
    }
}

